# -*- coding: utf-8 -*-
# @Time    : 2021/11/4 16:22
# @Author  : BrandonGT
# @FileName: bd_pyqt5.py
import logging
import sys

from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *


class BQHBoxLayout(QHBoxLayout):
    """默认设置margin和spacing"""
    def __init__(self):
        super(BQHBoxLayout, self).__init__()
        self.setContentsMargins(0, 0, 0, 0)
        self.setSpacing(0)


class BQVBoxLayout(QVBoxLayout):
    """默认设置margin和spacing"""
    def __init__(self):
        super(BQVBoxLayout, self).__init__()
        self.setContentsMargins(0, 0, 0, 0)
        self.setSpacing(0)


class BQWidget(QWidget):
    """默认设置StyledBackground"""
    def __init__(self):
        super(BQWidget, self).__init__()
        self.setAttribute(Qt.WA_StyledBackground)
        self.setContentsMargins(0, 0, 0, 0)


class BQTabWidget(QTabWidget):
    """默认设置Stylebackground"""
    def __init__(self):
        super(BQTabWidget, self).__init__()
        self.setAttribute(Qt.WA_StyledBackground)
        self.setContentsMargins(0, 0, 0, 0)


class BQPlainTextEdit(QPlainTextEdit):
    """默认设置Stylebackground"""
    def __init__(self):
        super(BQPlainTextEdit, self).__init__()
        self.setAttribute(Qt.WA_StyledBackground)
        self.setContentsMargins(0, 0, 0, 0)
        self.setReadOnly(True)


class BQPlainTextEditLogger(logging.Handler):
    """"QPlainTextEdit专用Logger输出类"""
    def __init__(self):
        super(BQPlainTextEditLogger, self).__init__()
        self.widget = BQPlainTextEdit()
        self.widget.setReadOnly(True)
        sys.stdout = Consoling(text_signal=self.output_std)
        sys.stderr = Consoling(text_signal=self.output_std)

    def emit(self, record):
        msg = self.format(record)
        self.widget.appendPlainText(msg)

    def output_std(self, text):
        """输出sys std信息到控制台"""
        self.widget.appendPlainText(text)


class BQListWidget(QListWidget):
    """自定义列表事件"""
    def __init__(self):
        super(BQListWidget, self).__init__()
        self.current_app = ""

    def get_app(self, item, text):
        self.current_app = item.app.get(text)
        return self.current_app


class BQListWidgetItem(QListWidgetItem):
    """自定义存储app信息"""
    def __init__(self):
        super(BQListWidgetItem, self).__init__()
        self.app = {}

    def store_app(self, label, app):
        """储存item和路径对应信息"""
        self.app[label] = app


class BQComboBox(QComboBox):
    """自定义储存plugin信息"""
    def __init__(self):
        super(BQComboBox, self).__init__()
        self.plug = {}
        self.path = {}

    def store_plug(self, label, plug):
        """储存cb对应的label和地址"""
        self.plug[label] = plug

    def store_path(self, label, fpath):
        """储存package读取路径"""
        self.path[label] = fpath


class BQPushButton(QPushButton):
    """覆写鼠标按键事件"""
    def __init__(self, parent):
        super().__init__()
        self.widget = parent

    def mousePressEvent(self, evt):
        # 打开鼠标跟踪事件
        self.setMouseTracking(True)
        # 记录鼠标点击时的世界坐标
        self.global_x = evt.globalX()
        self.global_y = evt.globalY()
        # 记录鼠标点击时，窗口的世界坐标
        self.origin_x = self.widget.x()
        self.origin_y = self.widget.y()
        self.pressed = evt.button()

    def mouseReleaseEvent(self, evt):
        self.setMouseTracking(False)

    def mouseMoveEvent(self, evt):
        # 计算鼠标移动后，在世界坐标中的移动距离
        move_x = self.global_x - evt.globalX()
        move_y = self.global_y - evt.globalY()
        # 计算实际窗口需要移动的距离
        final_x = self.origin_x - move_x
        final_y = self.origin_y - move_y
        # 移动窗口 条件：如果点击的是左键
        if self.pressed == 1:
            self.widget.move(final_x, final_y)


class Consoling(QObject):
    """输出控制台信号"""
    text_signal = pyqtSignal(str)

    def write(self, text):
        """发射信号"""
        self.text_signal.emit(str(text))
        QApplication.processEvents()


class BQssTool(object):
    @staticmethod
    def set_qss(fpath, obj):
        with open(fpath, "r") as f:
            content = f.read()
        obj.setStyleSheet(content)


if __name__ == '__main__':
    pass
